﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Linq;

using C1.Phone;
using C1.Phone.FlexGrid;

namespace ControlExplorer.Controls.FlexGrid
{
    public partial class FlexGridFilter : UserControl
    {
        private C1FlexGrid _flexGrid;
        private List<ColumnFilter> _currentFilters;
        private C1Popup _parentPopup;

        public FlexGridFilter(C1FlexGrid flexGrid, C1Popup parentPopup)
        {
            InitializeComponent();

            _flexGrid = flexGrid;
            _parentPopup = parentPopup;
            _currentFilters = new List<ColumnFilter>();

            foreach (Column col in _flexGrid.Columns)
            {
                if (col.DataType == typeof(string))
                {
                    _currentFilters.Add(new ColumnFilter() { Column = col, Value = string.Empty });
                }
            }
            _flexGridColumns.ItemsSource = _currentFilters;
        }

        private void Clear(object sender, RoutedEventArgs e)
        {
            foreach (var filter in _currentFilters)
            {
                filter.Value = string.Empty;
            }
            _flexGrid.CollectionView.Filter = null;
            _parentPopup.IsOpen = false;
        }

        private void Filter(object sender, RoutedEventArgs e)
        {
            if (_currentFilters.Where(f => !string.IsNullOrEmpty(f.Value.Trim())).Count() > 0)
            {
                _flexGrid.CollectionView.Filter = FilterPredicate;
            }
            else
            {
                _flexGrid.CollectionView.Filter = null;
            }
            _parentPopup.IsOpen = false;
        }

        // predicate used to filter items in CollectionView
        private bool FilterPredicate(object item)
        {
            foreach (var colFilter in _currentFilters)
            {
                string filterValue = colFilter.Value.Trim().ToUpper();
                if ((colFilter.Column.PropertyInfo != null) && !string.IsNullOrEmpty(filterValue))
                {
                    string value = colFilter.Column.PropertyInfo.GetValue(item, null) as string;
                    // use String.Contains method for filter sample
                    if (value == null || !value.ToUpper().Contains(filterValue))
                    {
                        return false;
                    }
                }
            }
            return true;
        }
    }

    public class ColumnFilter : INotifyPropertyChanged
    {
        private Column _column;
        public Column Column
        {
            get
            {
                return _column;
            }
            set
            {
                if (_column != value)
                {
                    _column = value;
                    OnPropertyChanged("Column");
                }
            }
        }

        private String _value;
        public String Value
        {
            get
            {
                return _value;
            }
            set
            {
                if (_value != value)
                {
                    _value = value;
                    OnPropertyChanged("Value");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
